//~ Project: wslds-blog File: BaseService.java Created date: Feb 20, 2012
package com.wslds.base;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.Table;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;

import com.wslds.base.dao.BaseDaoImpl;
import com.wslds.base.dao.page.PageAdapter;

/**
 * 
 * @author $Author: donilan.qq@gmail.com $
 * @version $Revision: 83 $
 */
public abstract class AbstractService<T> implements InitializingBean {

	private static final Log LOG = LogFactory.getLog(AbstractService.class);

	@Resource
	protected BaseDaoImpl dao;

	protected String tableName;

	protected Class<T> entityClass;

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void afterPropertiesSet() {
		Type genType = getClass().getGenericSuperclass();
		Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
		entityClass = (Class) params[0];

		Table table = entityClass.getAnnotation(Table.class);
		if (table == null) {
			LOG.error(String.format("class [%s] 没有@Table注解.",
					entityClass.getCanonicalName()));
		}
		tableName = table.name();
	}

	public void setDao(BaseDaoImpl dao) {
		this.dao = dao;
	}

	/* 当前数据库表 */

	public void save(T t) {
		this.dao.save(tableName, t);
	}

	public void update(T t) {
		this.dao.update(tableName, t);
	}

	public void delete(T t) {
		this.dao.delete(tableName, t);
	}

	public List<T> find(T t) {
		return this.dao.find(tableName, t);
	}

	public List<T> findExt(T t) {
		return this.dao.findExt(tableName, t);
	}

	public T findOne(T t) {
		return this.dao.findOne(tableName, t);
	}

	public T findOneExt(T t) {
		return this.dao.findOneExt(tableName, t);
	}

	public List<T> list(T t, int offset, int limit) {
		return this.dao.list(tableName, t, offset, limit);
	}

	public PageAdapter<T> page(T t, int page, int limit) {
		return this.dao.page(tableName, t, page, limit);
	}

	public PageAdapter<T> page(T t, int page) {
		return this.dao.page(tableName, t, page);
	}

	/* 其它数据库表 */
	public <O> void save(String table, O o) {
		this.dao.save(table, o);
	}

	public <O> void update(String table, O o) {
		this.dao.update(table, o);
	}

	public <O> void delete(String table, O o) {
		this.dao.delete(table, o);
	}

	public <O> List<O> find(String table, O o) {
		return this.dao.find(table, o);
	}

	public <O> List<O> findExt(String sqlId, O o) {
		return this.dao.findExt(sqlId, o);
	}

	public <O> O findOne(String table, O o) {
		return this.dao.findOne(table, o);
	}

	public <O> O findOneExt(String sqlId, O o) {
		return this.dao.findOneExt(sqlId, o);
	}

	public <O> List<O> list(String table, O o, int offset, int limit) {
		return this.dao.list(table, o, offset, limit);
	}

	public <O> PageAdapter<O> page(String table, O o, int page, int limit) {
		return this.dao.page(table, o, page, limit);
	}

	public <O> PageAdapter<O> page(String table, O o, int page) {
		return this.dao.page(table, o, page);
	}

	public <O> PageAdapter<O> pageExt(String sqlId, O o, int page, int limit) {
		return this.dao.pageExt(sqlId, o, page, limit);
	}

	public <O> PageAdapter<O> pageExt(String sqlId, O o, int page) {
		return this.dao.pageExt(sqlId, o, page);
	}
}
